<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/22/022
 * Time: 10:40
 */

namespace app\api\service;


use think\Controller;
use think\Exception;

class Comment extends Controller {

    /**
     * 保存评论信息
     * @param $param
     * @return array
     */
    public static function save_comment($param) {
        try {
            // ** 获取订单信息
            $order = model('order')->get(['id' => $param['order_id'], 'member_id' => $param['member_id']]);
            if (!$order) {
                throw new Exception('参数错误');
            }
            if ($order->comment_status == 2) {
                throw new Exception('订单已评论');
            }
            $data = [];
            foreach ($param['comments'] as $k => $v) {
                if(isset($v['img']) && is_array($v['img'])) {
                    $i = 1;
                    foreach ($v['img'] as $img) {
                        $var = 'img_' . $i;
                        $$var = $img. '-500';
                        $i++;
                    }
                } else {
                    $img_1 = $v['img_1'] ?  $v['img_1']. '-500' : '';
                    $img_2 = $v['img_2'] ?  $v['img_2']. '-500' : '';
                    $img_3 = $v['img_3'] ?  $v['img_3']. '-500' : '';
                    $img_4 = $v['img_4'] ?  $v['img_4']. '-500' : '';
                    $img_5 = $v['img_5'] ?  $v['img_5']. '-500' : '';
                }

                $data[] = [
                    'member_id' => $param['member_id'],
                    'shop_id' => $order->shop_id,
                    'order_id' => $param['order_id'],
                    'product_id' => $v['product_id'],
                    'level' => $v['level'],
                    'content' => $v['content'],
                    'service_level' => $v['service_level'],
                    'img_1' => isset($img_1) ? $img_1 : '',
                    'img_2' => isset($img_2) ? $img_2 : '',
                    'img_3' => isset($img_3) ? $img_3 : '',
                    'img_4' => isset($img_4) ? $img_4 : '',
                    'img_5' => isset($img_5) ? $img_5 : '',
                    'is_anonymity' => $param['is_anonymity']
                ];
            }
            // ** 保存品论信息
            model('comment')->saveAll($data);

            // ** 更新订单评论状态
            model('order')->save(['comment_status' => 2], ['id' => $param['order_id']]);

            return api_return(200, []);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 获取产品评论信息
     * @param $param
     * @return array
     */
    public static function get_comment_by_shop_and_product($param) {
        $comment = model('comment')->where(['product_id' => $param['id'], 'shop_id' => $param['shop_id']])->order('id desc')->paginate();

        $data['list'] = [];
        foreach ($comment as $k => $v) {
            $images = [];
            $v->img_1 ? $images[] = get_img_url($v->img_1) : $a = '';
            $v->img_2 ? $images[] = get_img_url($v->img_2) : $a = '';
            $v->img_3 ? $images[] = get_img_url($v->img_3) : $a = '';
            $v->img_4 ? $images[] = get_img_url($v->img_4) : $a = '';
            $v->img_5 ? $images[] = get_img_url($v->img_5) : $a = '';
            $data['list'][] = [
                'headimgurl' => get_img_url($v->member->memberRoleInfo->headimgurl),
                'nickname' => $v->member->memberRoleInfo->nickname,
                'level' => $v->level,
                'create_time' => $v->create_time,
                'msg' => $v->content,
                'images' => $images,
                'product_name' => $v->product->name,
            ];
        }
        $comment = $comment->toArray();
        $is_more = $comment['total'] - ($comment['current_page'] * $comment['per_page']);
        $data['is_more'] = $is_more;
        return api_return(200, $data);
    }
}